package com.franklin.validation.core.support;

import com.franklin.validation.core.annotations.WebFluxFormDataComponent;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.filter.AnnotationTypeFilter;


import java.util.Set;

/**
 * Description :
 * Create By: Franklin
 * Date : 2020/6/16 16:07
 */
public class WFFDComponentPackageScan implements ImportBeanDefinitionRegistrar, ResourceLoaderAware {

    private final static String PATH = "com.franklin.validation.core";

    private ResourceLoader resourceLoader;

    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }

    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        new ClassPathBeanDefinitionScanner(registry,false){
            @Override
            protected Set<BeanDefinitionHolder> doScan(String... basePackages) {
                super.addIncludeFilter(new AnnotationTypeFilter(WebFluxFormDataComponent.class));
                return super.doScan(basePackages);
            }
        }.doScan(PATH);
    }

}
